package com.gxa.clientorder.service.impl;

import com.gxa.clientorder.exception.BusinessException;
import com.gxa.clientorder.mapper.GroupbuyMapper;
import com.gxa.clientorder.pojo.Groupbuy;
import com.gxa.clientorder.service.GroupbuyService;
import com.gxa.clientorder.utils.CreateNumber;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author FanJiang
 * @Date 2020/11/16
 * @Version 1.0
 */
@Service("groupbuyService")
public class GroupbuyServiceImpl implements GroupbuyService {

    @Resource
    GroupbuyMapper groupbuyMapper;

    /**
     * 团购列表接口
     * @param gronum 团购编号
     * @param state 团购状态
     * @return list
     */
    @Override
    public List findList(String gronum, Integer state) {
        Example example = new Example(Groupbuy.class);
        Example.Criteria criteria = example.createCriteria();
        if (gronum != null){
            criteria.orLike("gronum", gronum  + "%");
//            criteria.andEqualTo("gronum", gronum  + "%");
        }
        if (state != null && state != 0){
            criteria.andEqualTo("state",state);
        }
        return groupbuyMapper.selectByExample(example);
    }

    /**
     * 查看拼团详情接口中的拼团表信息
     * @param id
     * @return Groupbuy
     */
    @Override
    public Groupbuy findById(Integer id) {
        Groupbuy groupbuy = new Groupbuy();
        groupbuy.setId(id);
        return groupbuyMapper.selectByPrimaryKey(groupbuy);
    }

    /**
     * 查看拼团详情接口中的拼团表信息
     * @param gronum
     * @return Groupbuy的list
     */
    @Override
    public List findBygronum(String gronum) {
        Groupbuy groupbuy = new Groupbuy();
        groupbuy.setGronum(gronum);
        return groupbuyMapper.select(groupbuy);
    }

//    /**
//     * 查看拼团详情接口中的商品表信息
//     * @param sysnum
//     * @return Product
//     */
//    @Override
//    public Product findProBynum(String sysnum) {
//        return groupbuyMapper.findProBynum(sysnum);
//    }


    /**
     * 开团插入
     * @param sysnum 商品编号
     * @param username 购买用户昵称
     * @param grocount 团总限制人数
     * @return int
     */
    @Override
    public Groupbuy opengro(String sysnum, String username, Integer grocount) {
        Groupbuy groupbuy = new Groupbuy();
        //生成拼团流水号
        String gronum = CreateNumber.getNum();
        groupbuy.setGronum(gronum);
        groupbuy.setSysnum(sysnum);
        groupbuy.setGrousername(username);
        groupbuy.setUsername(username);
        //设置团购总人数
        groupbuy.setGrocount(grocount);
        //设置团购还差人数
        groupbuy.setLack(grocount - 1);
        //设置状态，1、待付款，2、拼团中,3/拼团成功
        groupbuy.setState(1);
        groupbuy.setStarttime(new Date());
        int i = groupbuyMapper.insertSelective(groupbuy);
        if (i == 0){
            throw new BusinessException("开团失败");
        }
        Groupbuy groupbuy2 = new Groupbuy();
        groupbuy2.setGronum(gronum);
        return groupbuyMapper.selectOne(groupbuy2);
    }


    /**
     * 开团插入
     * @param groupbuy
     * @return int
     */
    @Override
    public Groupbuy joingro(Groupbuy groupbuy) {
        //1、待付款
        groupbuy.setState(1);
        int i = groupbuyMapper.insertSelective(groupbuy);
        if (i == 0){
            throw new BusinessException("参团失败");
        }
        return null;
    }

    /**
     * 更新支付状态
     * @param groid id作为唯一识别号
     * @param state 支付状态，成功传参2，失败传参1，取消传参4
     * 1/待付款、2/拼团中、4/拼团失败、
     * @return
     */
    @Override
    public Integer updatePay(Integer groid, Integer state) {
        Groupbuy groupbuy = new Groupbuy();
        groupbuy.setId(groid);
        //判断订单状态是否为失败，如果为失败，则更改订单号为null
        if (state == 4){
            groupbuy.setGronum(null);
        }
        groupbuy.setState(state);
        return groupbuyMapper.updateByPrimaryKeySelective(groupbuy);
    }
}
